#ifndef QNBUSERINFO_H
#define QNBUSERINFO_H

#include <QObject>
#include <QDateTime>
#include <QDebug>

#include "QNBUtls.h"
#include "QNBConfig.h"


//如果超过一定时间，没有心跳迹象，则退出，锁频。 暂定5分钟
#define HEART_INTERVAL_MAX_TIME (1*60)

enum LOGIN_STATUS {
  ENUM_LOGIN_BEGIN,
  ENUM_LOGIN,
  ENUM_LOGIN_END
};


enum FORCE_ACTION {
    ENUM_FORCE_LOGIN,
    ENUM_FORCE_LOGOUT,
    ENUM_FORCE_UNKNOW
};


class QNBUserInfo:public QObject
{
    Q_OBJECT

public:
    QNBUserInfo();

    static QNBUserInfo* getInstance();

    LOGIN_STATUS mLoginStatus;
    FORCE_ACTION mForceAction;/*后台强制动作，强制登陆，强制登出 */
    QString mstrIdentify;
    QString mstrClientId;
    QString mstrAuthPhoto;/* 请求认证图片的路径 */
    QString mstrLocalAuthPhoto; /*保存到本地的认证图片路径 */
    QString mstrRealPhoto;

    long miLoginTime;//登陆时间
    long miHeartTime;//心跳时间
    long miLogoutTime;//登出时间


    //实时图片比对的相识度
    int miRealCheckPhotoSimilar;

    //连续比对图片错误次数
    int miCheckPhotoErrorCount;//登陆之后，的比对

    int miLoginCheckPhotoErrorCount;//登陆时比对错误次数，如果超过一定次数，提示到柜台重新认证。

    bool isForceLogin()
    {
        bool bRet = false;
        if(mForceAction == ENUM_FORCE_LOGIN && !mstrIdentify.isEmpty()) {
            mForceAction = ENUM_FORCE_UNKNOW;
            bRet = true;
        }
        return bRet;
    }

    bool isForceLogout()
    {
        bool bRet = false;
        if(mForceAction == ENUM_FORCE_LOGOUT) {

            bRet = true;
        }
        return bRet;
    }

    bool isHeartTimeOut()
    {
        bool bRet = false;
        if(mLoginStatus == ENUM_LOGIN && ((QNBUtls::curTime() - miHeartTime) > HEART_INTERVAL_MAX_TIME)) {

            qDebug()<<QNBUtls::curTime() << miHeartTime;
            bRet = true;
        }

        return bRet;
    }

    bool isUp2CheckPhotoErrorMaxCount()
    {
        bool bRet = false;
        if(mLoginStatus == ENUM_LOGIN
                && miCheckPhotoErrorCount >= QNBConfig::getInstance()->lockscreenJudgeMaxCount())
        {
            bRet = true;
        }

        return bRet;
    }

    bool isDeleteUserDataTimeOut()
    {
        bool bRet = false;
        time_t cur = QNBUtls::curTime();
        //已经登出，且超过一定时间，则删除原来的登陆用户数据
        if (mLoginStatus == ENUM_LOGIN
                && ((cur - this->miLogoutTime) >= QNBConfig::getInstance()->deleteUserDataDelayTime())) {
            bRet = true;
        }

        return bRet;
    }

    void clearCheckPhotoErrorCount()
    {
        miCheckPhotoErrorCount = 0;
    }

    void setLoginStatus(LOGIN_STATUS loginStatus)
    {
        if(loginStatus != mLoginStatus) {
            mLoginStatus = loginStatus;
        }
    }

//    /* 判断相似度是否通过检测*/
    bool isCheckPhotoPass()
    {
        bool bRet = false;
        if(miRealCheckPhotoSimilar >= QNBConfig::getInstance()->checkPhotoSimilar()) {
            this->miCheckPhotoErrorCount = 0;
            bRet = true;
        }

        return bRet;
    }

    void clear();
    bool deleteUserData();


private:
    void test();

    QString getCpuId();

private:
    static QNBUserInfo *gUserInfo;

};

#endif // QNBUSERINFO_H
